
const app = getApp()

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		publisherTypePicker: ['普通', '经纪人', '中介'],
		publisherType: 0,
		title:'',
		contacter:'',
		contactPhone:'',
		houseType:'',
		houseLivingRoom:'',
		price:'',
		floor:'',
		totalFloor:'',
		address:'',
		detail:'',
		photos: [],
		cityPicker: [],
		cityCode: [],
		districtPicker: [],
		districtCode: [],
		siteId: 340100,
		index: 0,
		index2: 0,
		propertyType: 0,
		propertyTypePicker: ['住宅', '商铺', '写字楼'],
		fitmentPicker: ['毛坯', '普通装修', '精装修'],
		fitment: 0,
		seeHouseTimePicker: ['随时', '非工作时间', '预约'],
		seeHouseTime: 0,
		aId: 0,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let cityCode = wx.getStorageSync('cityCode')
		if (cityCode != undefined && cityCode != '') {
			this.setData({
				siteId: cityCode
			})
		}

		this.getCity()
		this.getDistrict()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	handlePublisherTypePicker(e) {
		this.setData({
			publisherType: e.detail.value
		})
	},

	handleTitle(e) {
		this.setData({
			title: e.detail.value
		})
	},

	handleContacter(e) {
		this.setData({
			contacter: e.detail.value
		})
	},

	handleContactPhone(e) {
		this.setData({
			contactPhone: e.detail.value
		})
	},

	handleHouseType(e) {
		this.setData({
			houseType: e.detail.value
		})
	},
	handleHouseLivingRoom(e) {
		this.setData({
			houseLivingRoom: e.detail.value
		})
	},
	handleHouseArea(e) {
		this.setData({
			houseArea: e.detail.value
		})
	},
	handlePrice(e) {
		this.setData({
			price: e.detail.value
		})
	},
	handleFloor(e) {
		this.setData({
			floor: e.detail.value
		})
	},
	handleTotalFloor(e) {
		this.setData({
			totalFloor: e.detail.value
		})
	},
	handleAddress(e) {
		this.setData({
			address: e.detail.value
		})
	},
	handleDetail(e) {
		this.setData({
			detail: e.detail.value
		})
	},

	handleSubmit() {
		let vm = this;
		let title = vm.data.title;
		let contactPhone = vm.data.contactPhone;
		let contacter = vm.data.contacter
		let price = vm.data.price

		if (title == '') {
			wx.showToast({
				title: '标题不能为空',
				icon: 'none',
				duration: 2000
			});
			return false;
		} else if (!(/^[\u4E00-\u9FA5]{2,4}$/.test(contacter))) {
			wx.showToast({
				title: '姓名格式错误！请重新输入',
				icon: 'none',
				duration: 2000
			});
			return false;
		} else if (price == ''||price < 0) {
			wx.showToast({
				title: '价格错误',
				icon: 'none',
				duration: 2000
			});
			return false;
		} else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(contactPhone))) {
			wx.showToast({
				title: '手机号码格式错误！请重新输入',
				icon: 'none',
				duration: 2000
			});
			return false;
		} else {

			let open_id = wx.getStorageSync('user_new')

			let data = this.data
			data['open_id'] = open_id
			app.core('post', 'oldhouse/add', data, (res) => {
					console.log(res)
					if (res.resultCode == 200) {
						wx.showToast({
							title: '发布成功',
							icon: 'success',
							duration: 2000
						});
						setTimeout(function () {
							wx.navigateBack({
								delta: 1
							})
						}, 1000)
					} else {
						wx.showToast({
							title: '发布失败！' + res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				},
				(err) => {
					console.log(err)
				}
			)

		}

	},

	uploadimg(url) {
		var that = this;

		wx.showLoading({
			title: '上传中',
		});
		console.log(url)
		wx.uploadFile({
			url: 'https://xkcloude.hfhouse.com/routineclient/upload_file',
			filePath: url[0],
			name: 'photo_url',
			success(res) {
				console.log(res)
				var x = JSON.parse(res.data);
				console.log(x)
				if (x.resultCode == 500) {
					wx.showToast({
						title: x.data.msg,
						icon: 'none',
						duration: 3000
					})
					return
				}else if(x.resultCode == 200) {
					wx.showToast({
						title: '上传成功',
						icon: 'success',
						duration: 2000
					})

					that.setData({
						photos: that.data.photos.concat(x.data.album)
					})
				}
				wx.hideLoading()
			}
		})
	},

	chooseImage() {
		wx.chooseImage({
			count: 4, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: (res) => {
				this.uploadimg(res.tempFilePaths)
			}
		});
	},
	viewImage(e) {
		wx.previewImage({
			urls: this.data.photos,
			current: e.currentTarget.dataset.url
		});
	},
	delImg(e) {
		wx.showModal({
			title: '提示',
			content: '确定要删除吗？',
			cancelText: '取消',
			confirmText: '确定',
			success: res => {
				if (res.confirm) {
					this.data.photos.splice(e.currentTarget.dataset.index, 1);
					this.setData({
						photos: this.data.photos
					})
				}
			}
		})
	},

	//获取城市
	getCity: function() {
		// wx.showLoading({
		//   title: '加载中',
		// })

		let vm = this;
		let siteId = this.data.siteId
		app.core('get', 'get_all_sites', {}, (res) => {
				var picker = vm.data.picker;
				var citylist = [];
				var showcitylist = [];
				for (var i = 0; i < res.data.length; i++) {
					citylist.push({
						name: res.data[i].area,
						id: res.data[i].id
					});
					showcitylist.push(res.data[i].area);

					if (siteId > 0 && res.data[i].id == siteId) {
						vm.setData({
							index: i
						})
					}
				}

				vm.setData({
					cityPicker: showcitylist ,
					cityCode: citylist,
				});
				// wx.hideLoading()
			},
			(err) => {
				console.log(err)
			}
		)
	},

	handleCityPicker: function (e) {
		this.setData({
			index: e.detail.value,
			siteId: this.data.cityCode[e.detail.value]['id'],
		})
		this.getDistrict()
	},

	handleDistrictPicker: function (e) {
		this.setData({
			index2: e.detail.value,
			aId: this.data.districtCode[e.detail.value]['id'],
		})
	},

	getDistrict: function(e) {
		let vm = this;
		let siteId = this.data.siteId
		app.core('get', 'district?siteId=' + siteId, {}, (res) => {
				var citylist = [];
				var showcitylist = [];
				for (var i = 0; i < res.data.length; i++) {
					citylist.push({
						name: res.data[i]['areaName'],
						id:res.data[i]['id'],
					});
					showcitylist.push(res.data[i]['areaName']);

					if (vm.data.aId == 0) {
						console.log(res.data[i]['id'])
						vm.setData({
							aId: res.data[i]['id'],
						});
					}
				}

				vm.setData({
					districtPicker: showcitylist ,
					districtCode: citylist,
				});
				// wx.hideLoading()
			},
			(err) => {
				console.log(err)
			}
		)
	},

	handlePropertyTypePicker: function (e) {
		this.setData({
			propertyType: e.detail.value,
		})
	},

	handleFitmentPicker: function (e) {
		this.setData({
			fitment: e.detail.value,
		})
	},

	handleSeeHouseTimePicker: function (e) {
		this.setData({
			seeHouseTime: e.detail.value,
		})
	}
})
